package com.bkgy.controller.allprocessDefect.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.controller.allprocessDefect.dao.AllprocessDefectRequest;
import com.bkgy.controller.allprocessDefect.dao.AllprocessDefectResponse;
import com.bkgy.controller.allprocessDefect.mapper.AllprocessDefectMapper;
import com.bkgy.controller.allprocessDefect.service.AllprocessDefectService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


@Service

public class AllprocessDefectServiceImpl implements AllprocessDefectService {


    @Autowired
    private AllprocessDefectMapper allprocessDefectMapper;

    /**
     * 成材率和质量缺陷关联
     * @param request_get AllprocessDefectRequest
     * @return ResultData<AllprocessDefectResponse>
     */
    public  ResultData<AllprocessDefectResponse> getDefectDate(AllprocessDefectRequest request_get) {
        //日期转换与赋值
        AllprocessDefectRequest request = setdatetime(request_get);

        try
        {
            Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());

            //行数
            int count = 0;

            List<AllprocessDefectResponse> results_list = new ArrayList<>();
            if(request.getLinename()==null)
            {
                request.setLinename("cal1630");
            }
            else if(request.getLinename().equals("cal1630"))
            {
                results_list = allprocessDefectMapper.getDefecCal1630(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("cal2150"))
            {
                results_list = allprocessDefectMapper.getDefecCal2150(page, request);
                count = (int)page.getTotal();
            }
            else if(request.getLinename().equals("cgl1870"))
            {
                results_list = allprocessDefectMapper.getDefecCgl1870(page, request);
                count = (int)page.getTotal();
            }

            return ResultData.ResultDataSuccess(results_list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    /**
     * 根据前台传入查询开始和查询结束日期，构造出yyyymmddhhmmss数据查询
     * @param request_get AllprocessDefectRequest
     * @return AllprocessDefectRequest
     */
    public AllprocessDefectRequest setdatetime(AllprocessDefectRequest request_get) {
        try {
            String[] array_startTime = request_get.getDbegin().split("-");//时间格式：2025-06-01 00:00:01

            String dbegin_Times = "";//时间格式：20250703075254
            if (array_startTime.length > 0) {
                //构建时间格式：20250703075254
                String times2 = array_startTime[2].replace(" ", "");
                times2 = times2.replace(":", "");
                dbegin_Times = array_startTime[0] + array_startTime[1] + times2;
            }
            if(dbegin_Times.length()>=14)
            {
                dbegin_Times=dbegin_Times.substring(0,14);
            }
            request_get.setDbegin(dbegin_Times);//时间格式：20250703075254

            String[] array_endTime = request_get.getDend().split("-");//时间格式：2025-06-01 00:00:01
            String endTime_Times="";//时间格式：20250703075254
            if (array_endTime.length > 0) {
                //构建时间格式：20250703075254
                String times2=array_endTime[2].replace(" ","");
                times2 = times2.replace(":","");
                endTime_Times=array_endTime[0]+array_endTime[1]+times2;
            }
            if(endTime_Times.length()>=14)
            {
                endTime_Times=endTime_Times.substring(0,14);
            }
            request_get.setDend(endTime_Times);//时间格式：20250703075254

        } catch (Exception e) {
            e.printStackTrace();
        }
        return request_get;
    }
}
